package cn.itlight.org.controller;

import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.utils.PageList;
import cn.itlight.org.domain.Department;
import cn.itlight.org.query.DepartmentQuery;
import cn.itlight.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
* @Description: controller层
* @Author: TomLiu
* @Version: 1.0
* @Date:  2022/6/24 10:07
*/
@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /*
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:07
    * @Parameters:
    * @Return
    */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /*
    * @Title: loadById
    * @Description: 根据id查询
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:07
    * @Parameters:
    * @Return
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department loadById(@ApiParam(value="ID属性",required = true) @PathVariable Long id){
        return departmentService.loadById(id);
    }

    /*
    * @Title: remove
    * @Description: 根据id删除
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:59
    * @Parameters:
    * @Return
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

    /*
    * @Title: addOrUpt
    * @Description: 新增/修改
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 11:03
    * @Parameters:
    * @Return
    */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            } else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

    /*
    * @Title: addOrUpt
    * @Description: 高级查询/分页
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 11:05
    * @Parameters:
    * @Return
    */
    @PostMapping
    public PageList addOrUpt(@RequestBody DepartmentQuery department){
        return departmentService.queryData(department);
    }

}
